package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.UserDTO;
import com.carkeeper.service.OrderLogService;
import com.carkeeper.entity.OrderPO;
import com.carkeeper.entity.OrderLogPO;
import com.carkeeper.mapper.OrderLogMapper;
import com.carkeeper.mapper.OrderMapper;
import com.carkeeper.service.UserFeign;
import com.carkeeper.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class OrderLogServiceImpl implements OrderLogService {
    @Autowired
    private OrderLogMapper orderLogMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserFeign userFeign;

    /*
     * 新增订单日志
     * */
    @Override
    public void addOrderLog(OrderLogPO orderLogPO) {
        orderLogMapper.insert(orderLogPO);
    }

    /*
     * 获取订单日志分页数据
     * */
    @Override
    public Pager<OrderLogPO> getOrderLogPage(Page<OrderLogPO> page) {
        orderLogMapper.selectPage(page, null);
        Pager<OrderLogPO> pager = new Pager<>(page);
        return pager;
    }

    /*
     * 获取订单日志详情
     * */
    @Override
    public OrderLogPO getOrderLog(Long id) {
        OrderLogPO orderLogPO = orderLogMapper.selectById(id);
        OrderPO orderPO = orderMapper.selectById(orderLogPO.getOrderId());
        UserDTO user = userFeign.getUser(orderPO.getUserId());
        OrderPO orderByOrderLog = null;
        if (orderLogPO != null && orderLogPO.getId() != null) {
            orderByOrderLog = orderMapper.selectById(orderLogPO.getOrderId());
        }
        OrderPO order = new OrderPO();
        if (orderByOrderLog != null) {
            order.setId(orderByOrderLog.getId());
            order.setOrderStatus(orderByOrderLog.getOrderStatus());
        }
        if (orderLogPO != null && user != null) {
            orderLogPO.setOrder(order);
            orderLogPO.setUserDTO(user);
            return orderLogPO;
        }
        return null;
    }

    /*
     * 删除订单日志
     * */
    @Override
    public void delById(Long id) {
        orderLogMapper.deleteById(id);
    }

    /*
     * 根据订单订单id获取订单日志
     * */
    @Override
    public List<OrderLogPO> getByOrderId(Long orderId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("order_id", orderId);
        List<OrderLogPO> orderLogPOS = orderLogMapper.selectByMap(map);
        return orderLogPOS;
    }

}
